package hk.ust.comp3111.ustnutrition;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;

public class FoodListSearchDialog extends DialogFragment
{
	public static class AdvancedSearchCriteria
	{
		public String food;
		public String rest;
		public boolean isCarbohydrateRich;
		public boolean isProteinRich;
		public boolean isFatRich;
		public boolean isSodiumRich;
	}

	public interface FoodListSearchDialogListener
	{
		public void onAdvancedSearch(AdvancedSearchCriteria criteria);
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		if (getTargetFragment() instanceof FoodListSearchDialogListener)
		{
			mListener = (FoodListSearchDialogListener)getTargetFragment();
		}
		else
		{
			throw new IllegalStateException("Target fragment must be set");
		}

	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		ContextThemeWrapper theme = new ContextThemeWrapper(getActivity(),
				R.style.Theme_AppCompat_Light);
		LayoutInflater inflater = LayoutInflater.from(getActivity())
				.cloneInContext(theme);
		final View dialogView = inflater.inflate(R.layout.food_list_search_dialog, null);
		initView(dialogView);
		OnClickListener okListener = new OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						onAdvancedSearch();
					}
				};

		AlertDialog.Builder builder = new AlertDialog.Builder(theme);
		Dialog d = builder
				.setView(dialogView)
				.setPositiveButton(android.R.string.ok, okListener)
				.setNegativeButton(android.R.string.cancel, null)
				.create();
		return d;
	}

	private void initView(View root)
	{
		mFoodView = (TextView)root.findViewById(R.id.name);
		mRestView = (TextView)root.findViewById(R.id.rest);
		mCarbohydrateSwitch = (Switch)root.findViewById(R.id.carbo);
		mProteinSwitch = (Switch)root.findViewById(R.id.protein);
		mFatSwitch = (Switch)root.findViewById(R.id.fat);
		mSodiumSwitch = (Switch)root.findViewById(R.id.sodium);
	}

	private void onAdvancedSearch()
	{
		AdvancedSearchCriteria c = new AdvancedSearchCriteria();

		c.food = TextUtils.isEmpty(mFoodView.getText()) ? null
				: mFoodView.getText().toString();
		c.rest = TextUtils.isEmpty(mRestView.getText()) ? null
				: mRestView.getText().toString();
		c.isCarbohydrateRich = mCarbohydrateSwitch.isChecked();
		c.isProteinRich = mProteinSwitch.isChecked();
		c.isFatRich = mFatSwitch.isChecked();
		c.isSodiumRich = mSodiumSwitch.isChecked();

		mListener.onAdvancedSearch(c);
	}

	private FoodListSearchDialogListener mListener;

	private TextView mFoodView;
	private TextView mRestView;
	private Switch mCarbohydrateSwitch;
	private Switch mProteinSwitch;
	private Switch mFatSwitch;
	private Switch mSodiumSwitch;
}
